<?php
/**
 * Error Talk Class
 *
 * Give The Ability To Manage All Application Error Messages.
 *
 * @package             Utilities and Tools
 * @subpackage          Libraries
 * @category            Libraries
 * @author		MOHAMED ELMAHDY - WEB DEVELOPER
 * @version             1.0
 * @link		http://mohamedelmahdy.com/
 */

/**
 * all configuration  needs to be edit found at initialize function
 */
class errorTalk {

    public static $conf = array();

    public static function initialize() {
        //if E_STRICT is not defined, define it
        if(!defined('E_STRICT')) define('E_STRICT', 0);

        /*************************************************************************
       *
       * Send Errors to Specified Email Address ?
       * 
       * @var boolean
       *
       *************************************************************************
         */
        self::$conf['emailActive'] = FALSE;
        /*************************************************************************
       *
       * print error to browser
       * 
       * @var boolean
       *
       *************************************************************************
         */
        self::$conf['showErrorToBrowser'] = FALSE;
        /*************************************************************************
       *
       * print error to browser
       *
       * @var boolean
       *
       *************************************************************************
         */
        self::$conf['showErrorToDatabase'] = FALSE;
        /*************************************************************************
       *
       * Throwing errors in a specific file
       *
       * @var boolean
       *
       *************************************************************************
         */
        self::$conf['logFile'] = TRUE;
        /*************************************************************************
       *
       * File Path Errors
       * 
       * @var string
       *
       *************************************************************************
         */
        self::$conf['logFilePath'] = APSPATH."/log/errorLogFile.txt";
        /*************************************************************************
       *
       * E-mail that you want to tell him mistakes
       *
       * Note that this fueature will disable if self::$conf['Email'] is False
       *
       * @var string
       *
       *************************************************************************
         */
        self::$conf['Email'] = "pchanngam404@gmail.com";

        /*************************************************************************
       *
       * Webmaster email
       *
       * Note that this fueature will disable if self::$conf['Email'] is False
       * 
       * @var string
       *
       *************************************************************************
         */
        self::$conf['webmasterEmail'] = 'pchanngam404@gmail.com';
        /*************************************************************************
       *
       * Email header
       *
       * Note that this fueature will disable if self::$conf['Email'] is False
       * 
       * @var string
       *
       *************************************************************************
         */
        self::$conf['emailHeader'] = 'From : errorTalk ';
        /*************************************************************************
       *
       * Error Handler
       *
       * @var array
       *
       *************************************************************************
         */
        self::$conf['handlerName'] = array("errorTalk","errorHandler");
        /*************************************************************************
       *
       * E_COMPILE_ERROR default message
       *
       * @var string
       *
       *************************************************************************
         */
        self::$conf[E_COMPILE_ERROR] = "Fatal compile-time errors";
        /*************************************************************************
       *
       * E_COMPILE_WARNING default message
       *
       * @var string
       *
       *************************************************************************
         */
        self::$conf[E_COMPILE_WARNING] = "Compile-time warnings";
        /*************************************************************************
       *
       * E_CORE_ERROR default message
       *
       * @var string
       *
       *************************************************************************
         */
        self::$conf[E_CORE_ERROR] = "Fatal errors that occur during PHP's initial startup";
        /*************************************************************************
       *
       * E_CORE_WARNING default message
       *
       * @var string
       *
       *************************************************************************
         */
        self::$conf[E_CORE_WARNING] = "Warnings Generated By The Core Of PHP";
        /*************************************************************************
       *
       * E_DEPRECATED default message
       *
       * @var string
       *
       *************************************************************************
         */
        self::$conf[E_DEPRECATED] = "Deprecated Function";
        /*************************************************************************
       *
       * E_ERROR default message
       *
       * @var string
       *
       *************************************************************************
         */
        self::$conf[E_ERROR] = "Fatal run-time errors";
        /*************************************************************************
       *
       * E_PARSE default message
       *
       * @var string
       *
       *************************************************************************
         */
        self::$conf[E_PARSE] = "Compile-time parse errors";
        /*************************************************************************
       *
       * E_RECOVERABLE_ERROR default message
       *
       * @var string
       *
       *************************************************************************
         */
        self::$conf[E_RECOVERABLE_ERROR] = "Catchable fatal error";
        /*************************************************************************
       *
       * E_STRICT default message
       *
       * @var string
       *
       *************************************************************************
         */
        self::$conf[E_STRICT] = "System Suggest";
        /*************************************************************************
       *
       * E_WARNING default message
       *
       * @var string
       *
       *************************************************************************
         */
        self::$conf[E_WARNING] = "Run-time warnings";
        /*************************************************************************
       *
       * E_USER_ERROR default message
       *
       * @var string
       *
       *************************************************************************
         */
        self::$conf[E_USER_ERROR] = "User-generated error message";
        /*************************************************************************
       *
       * E_USER_NOTICE default message
       *
       * @var string
       *
       *************************************************************************
         */
        self::$conf[E_USER_NOTICE] = "User-generated notice message.";
        /*************************************************************************
       *
       * E_USER_WARNING default message
       *
       * @var string
       *
       *************************************************************************
         */
        self::$conf[E_USER_WARNING] = "User-generated warning message";
        /*************************************************************************
       *
       * E_USER_DEPRECATED default message
       *
       * @var string
       *
       *************************************************************************
         */
        self::$conf[E_USER_DEPRECATED] = "User-generated warning message";
        /*************************************************************************
       *
       * E_DEPRECATED default message
       *
       * @var string
       *
       *************************************************************************
         */
        self::$conf[E_ALL] = "Fatal Error";
        /*************************************************************************
       *
       * E_NOTICE default message
       *
       * @var string
       *
       *************************************************************************
         */
        self::$conf[E_NOTICE] = "Run-time notices";
        /*************************************************************************
       *
       * Error Level
       *
       * @var int
       *
       *************************************************************************
         */
        self::$conf['errorLevel'] = E_ALL | E_STRICT;
    }
    /*
    ****************************************************************************
    * @access            public
    * @param             int
    * @param             string
    * @param             string
    * @param             int
    * @param             array
    * @return            string
    ****************************************************************************
    */
    public static function errorHandler($errLevel,$errMessage,$errFile,$errLine,$errContext) {
        self::outputHandler($errLevel, $errMessage, $errFile, $errLine, $errContext);
    }
    /*
    ****************************************************************************
    * @access            public
    * @param             int
    * @param             string
    * @param             string
    * @param             int
    * @param             array
    * @return            string
    ****************************************************************************
    */
    private static function outputHandler($errLevel,$errMessage,$errFile,$errLine,$errContext) {
        $alertMsg  = "**************************************************************%errotalk-br%";
        $alertMsg .= "!Oops : [".self::getErrorLevel($errLevel)."] ($errMessage).%errotalk-br%";
        $alertMsg .= "Line Number  : $errLine.%errotalk-br%";
        $alertMsg .= "File Path : $errFile.%errotalk-br%";
        $alertMsg .= "Date : ".date('d.m.Y h:i:s').".%errotalk-br%";
        $alertMsg .= "**************************************************************%errotalk-br%";

        $output = str_replace ("%errotalk-br%", "<br>", $alertMsg); // write <br> tag to break line the lines with browsers

        if(self::$conf['showErrorToBrowser'])
            echo $output;

        $alertMsg = str_replace ("%errotalk-br%", "\r\n", $alertMsg); // write /r/n to break line with E-mail & Error File

        if(self::$conf['emailActive'])
            self::LogErrorToMail(strip_tags($alertMsg));

        if(self::$conf['logFile'])
            self::LogErrorToFile(strip_tags($alertMsg));

        if(self::$conf['showErrorToDatabase'])
            self::LogErrorToDatabase(self::getErrorLevel($errLevel), $errMessage, $errLine, strip_tags($errFile), date('Y-m-d H:i:s'));

    }
    private static function LogErrorToDatabase($errorType,
            $errorMsg,
            $errorLine,
            $errorFile,
            $errorDate) {
        $link = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
        mysql_select_db(DB_NAME);

        $result = mysql_query("INSERT INTO uap_sys_errors
                              (error_type, error_message,
                               error_line, error_file, error_date)
                               VALUES ('".$errorType."',
                                       '".$errorMsg."',
                                       '".$errorLine."',
                                       '".$errorFile."',
                                       '".$errorDate."')");
        mysql_close($link);
    }
    /*
    ****************************************************************************
    * @access            private
    * @param             string
    * @return            boolean
    ****************************************************************************
    */
    private static function LogErrorToMail($alertMsg) {
        @error_log($alertMsg, 1, self::$conf['Email'], self::$conf['emailHeader'].self::$conf['webmasterEmail']);
    }
    /*
    ****************************************************************************
    * @access            private
    * @param             string
    * @return            boolean
    ****************************************************************************
    */
    private static function LogErrorToFile($alertMsg) {
        error_log($alertMsg, 3, self::$conf['logFilePath']);
    }
    /*
    ****************************************************************************
    * @access            private
    * @param             int
    * @return            string
    ****************************************************************************
    */
    private static function getErrorLevel($errLevel) {
        switch ($errLevel) {
            case E_USER_ERROR :
                return self::$conf[E_USER_ERROR];
                break;
            case E_USER_NOTICE :
                return self::$conf[E_USER_NOTICE];
                break;
            case E_USER_WARNING :
                return self::$conf[E_USER_WARNING];
                break;
            case E_USER_DEPRECATED :
                return self::$conf[E_USER_DEPRECATED];
                break;
            case E_COMPILE_ERROR :
                return self::$conf[E_COMPILE_ERROR];
                break;
            case E_COMPILE_WARNING :
                return self::$conf[E_COMPILE_WARNING];
                break;
            case E_CORE_ERROR :
                return self::$conf[E_CORE_ERROR];
                break;
            case E_CORE_WARNING :
                return self::$conf[E_CORE_WARNING];
                break;
            case E_DEPRECATED :
                return self::$conf[E_DEPRECATED];
                break;
            case E_ERROR :
                return self::$conf[E_ERROR];
                break;
            case E_PARSE :
                return self::$conf[E_PARSE];
                break;
            case E_RECOVERABLE_ERROR :
                return self::$conf[E_RECOVERABLE_ERROR];
                break;
            case E_STRICT :
                return self::$conf[E_STRICT];
                break;
            case E_ALL :
                return self::$conf[E_ALL];
                break;
            case E_NOTICE :
                return self::$conf[E_NOTICE];
                break;
            case E_WARNING :
                return self::$conf[E_WARNING];
                break;
        }
    }
    /*
    ****************************************************************************
    *
    * start use errortalk functionality
    *
    * set the error handler , and specify the error level
    *
    * @access            public
    * @return            void
    ****************************************************************************
    */
    public static function errorTalk_Open() {
        set_error_handler(errorTalk::$conf['handlerName'],  errorTalk::$conf['errorLevel']);
    }
    /*
    ****************************************************************************
    *
    * stop use errortalk functionality
    *
    * hide all system error
    *
    * @access            public
    * @return            void
    ****************************************************************************
    */
    public static function errorTalk_Close() {
        set_error_handler(errorTalk::$conf['handlerName'],  FALSE);
        error_reporting(FALSE);
    }

}

?>
